package net.stogi.unicore.beyond.ui.client;

import java.util.HashMap;
import java.util.Map;

import net.stogi.common.gwt.frame.client.core.AbstractLayoutManager;

import com.extjs.gxt.ui.client.Style.LayoutRegion;
import com.extjs.gxt.ui.client.event.ButtonEvent;
import com.extjs.gxt.ui.client.event.SelectionListener;
import com.extjs.gxt.ui.client.util.Margins;
import com.extjs.gxt.ui.client.widget.Component;
import com.extjs.gxt.ui.client.widget.LayoutContainer;
import com.extjs.gxt.ui.client.widget.button.Button;
import com.extjs.gxt.ui.client.widget.layout.BorderLayout;
import com.extjs.gxt.ui.client.widget.layout.BorderLayoutData;
import com.extjs.gxt.ui.client.widget.layout.CardLayout;
import com.extjs.gxt.ui.client.widget.layout.HBoxLayout;
import com.extjs.gxt.ui.client.widget.layout.BoxLayout.BoxLayoutPack;
import com.google.gwt.core.client.GWT;
import com.google.gwt.user.client.ui.RootPanel;
import com.google.gwt.user.client.ui.Widget;

public class UnicoreBeyondUiLayoutManagerImpl extends AbstractLayoutManager {

	private final LayoutContainer mainContainer;

	private final LayoutContainer contentContainer;

	private final CardLayout contentLayout = new CardLayout();

	private final LayoutContainer editorContainer;

	private final LayoutContainer runnerContainer;

	private final Button editorButton = new Button("Task Editor");

	private final Button runnerButton = new Button("Task Runner");

	private final Map<String, LayoutContainer> sectionMap;

	public UnicoreBeyondUiLayoutManagerImpl() {
		mainContainer = new LayoutContainer(new BorderLayout());
		contentContainer = new LayoutContainer(contentLayout);
		editorContainer = new LayoutContainer(new BorderLayout());
		runnerContainer = new LayoutContainer(new BorderLayout());
		sectionMap = new HashMap<String, LayoutContainer>();

		editorButton.addSelectionListener(new SelectionListener<ButtonEvent>() {

			@Override
			public void componentSelected(ButtonEvent ce) {
				contentLayout.setActiveItem(editorContainer);
			}
		});
		runnerButton.addSelectionListener(new SelectionListener<ButtonEvent>() {

			@Override
			public void componentSelected(ButtonEvent ce) {
				contentLayout.setActiveItem(runnerContainer);
			}
		});

		initSections();
	}

	@Override
	public void addWidget(String section, Widget widget) {
		LayoutContainer container = sectionMap.get(section);

		if (container != null) {
			container.add(widget);
			container.add(doCreateSpacer(5));
		}
		else {
			GWT.log("LayoutManager.addWidget(): No section with given name is defined: [section = '" + section + "']", null);
		}
	}

	@Override
	public void layout() {
		RootPanel.get().remove(mainContainer);
		RootPanel.get().add(mainContainer);
	}

	@Override
	protected void initSections() {
		// Layout containers.
		HBoxLayout northLayout = new HBoxLayout();
		northLayout.setPack(BoxLayoutPack.END);

		BorderLayoutData contentData = new BorderLayoutData(LayoutRegion.CENTER, 1);

		LayoutContainer north = new LayoutContainer(northLayout);
		BorderLayoutData northData = new BorderLayoutData(LayoutRegion.NORTH, 38);
		northData.setMargins(new Margins(5, 5, 0, 5));
		sectionMap.put("north", north);

		mainContainer.add(north, northData);
		mainContainer.add(contentContainer, contentData);

		contentContainer.add(editorContainer);
		contentContainer.add(runnerContainer);

		addWidget("north", editorButton);
		addWidget("north", runnerButton);

		LayoutContainer editorWest = new LayoutContainer();
		BorderLayoutData editorWestData = new BorderLayoutData(LayoutRegion.WEST, .15f);
		editorWestData.setMargins(new Margins(5, 5, 5, 5));
		sectionMap.put("editor.west", editorWest);

		LayoutContainer editorCenter = new LayoutContainer();
		BorderLayoutData editorCenterData = new BorderLayoutData(LayoutRegion.CENTER, .65f);
		editorCenterData.setMargins(new Margins(5, 5, 5, 0));
		sectionMap.put("editor.center", editorCenter);

		LayoutContainer editorEast = new LayoutContainer();
		BorderLayoutData eastData = new BorderLayoutData(LayoutRegion.EAST, .20f);
		eastData.setMargins(new Margins(5, 5, 5, 0));
		sectionMap.put("editor.east", editorEast);

		editorContainer.add(editorWest, editorWestData);
		editorContainer.add(editorCenter, editorCenterData);
		editorContainer.add(editorEast, eastData);
		
		LayoutContainer runnerWest = new LayoutContainer();
		BorderLayoutData runnerWestData = new BorderLayoutData(LayoutRegion.WEST, .15f);
		runnerWestData.setMargins(new Margins(5, 5, 5, 5));
		sectionMap.put("runner.west", runnerWest);

		LayoutContainer runnerCenter = new LayoutContainer();
		BorderLayoutData runnerCenterData = new BorderLayoutData(LayoutRegion.CENTER, .85f);
		runnerCenterData.setMargins(new Margins(5, 5, 5, 0));
		sectionMap.put("runner.center", runnerCenter);
		
		runnerContainer.add(runnerWest, runnerWestData);
		runnerContainer.add(runnerCenter, runnerCenterData);
	}

	private Component doCreateSpacer(int size) {
		LayoutContainer spacer = new LayoutContainer();
		spacer.setHeight(size);
		return spacer;
	}
}
